package com.songjia.stream.consumer;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.SendTo;

import com.songjia.stream.model.User;

/**
 * 
 * 
 * @Description: 自定义消费者
 *
 * @author: songjia
 * @date: 2018年3月27日 上午11:00:11
 */
@EnableBinding(SendToBinder.class)
public class SendToReceiver {

	private static Logger logger = LoggerFactory.getLogger(SendToReceiver.class);

	@Value("${server.port}")
	private String port;

	/**
	 * 监听input通道
	 * 
	 * @param user
	 */
	@StreamListener(SendToBinder.INPUT) // 监听input通道
	@SendTo(SendToBinder.OUTPUT) // 返回处理结果到output通道
	// 此注解修饰的方法必须又返回值
	public String receive(User user) {
		try {
			user.setAge(new Random().nextInt(100));
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.info("SendToReceiver Received: {}, Port: {}", user.toString(), port);
		return user.toString();
	}
}
